package com.irdstudio.tdp.executor.core.plugin.util;

import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>Project:esaas.dmcenter				<p>
 * <p>Module :esaas.dmcenter.common			<p>
 * <p>Description: 表模型辅助类				<p>
 * @author guangming.li
 * @date 2017年08月30日
 */
public class TmModelUtil {
	
	/**
	 * 将表名转为Java类名
	 * @param tableCode
	 * @return
	 */
	public final static String tableCodeToClassName(String tableCode){
		
		// 只考虑表名中间带有_或-的情况
		String splitChar = "_";
		if (tableCode.indexOf('_') == -1) {
			splitChar = "-";
		}

		String names[] = tableCode.toLowerCase().split(splitChar);
		StringBuffer classNameBuffer = new StringBuffer();
		for (int i = 0; i < names.length; i++) {
			classNameBuffer.append(StringUtils.capitalize(names[i]));
		}
		return classNameBuffer.toString();	
	}
	
	/** 
     * 对象属性转换为字段  例如：userName to user_name 
     * @param property 字段名 
     * @return 
     */  
    public final static String propertyToField(String property) {  
        if (null == property) {  
            return "";  
        }  
        char[] chars = property.toLowerCase().toCharArray();  
        StringBuffer sb = new StringBuffer();  
        for (char c : chars) {  
            if (CharUtils.isAsciiAlphaUpper(c)) {  
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));  
            } else {  
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    }  
  
    /** 
     * 字段转换成对象属性 例如：user_name to userName 
     * @param field 
     * @return 
     */  
    public final static String fieldToProperty(String field) {  
        if (null == field) {  
            return "";  
        }  
        char[] chars = field.toLowerCase().toCharArray();  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < chars.length; i++) {  
            char c = chars[i];  
            if (c == '_') {  
                int j = i + 1;  
                if (j < chars.length) {  
                    sb.append(StringUtils.upperCase(CharUtils.toString(chars[j])));  
                    i++;  
                }  
            } else {  
                sb.append(c); 
            }  
        }  
        return sb.toString();  
    }
    
    /**
     * 将字段类型转换Java类型
     * @param fieldType
     * @return
     */
    public final static String fieldTypeToJavaType(String fieldType){
		if (StringUtils.isEmpty(fieldType))
			return "无效数据类型";
    	fieldType = fieldType.toUpperCase();
    	if("VARCHAR".equals(fieldType)
    			|| "VARCHAR2".equals(fieldType)
    			|| "CLOB".equals(fieldType)
    			|| "CHAR".equals(fieldType)
    			|| "NCHAR".equals(fieldType)
    			|| "CHAR2".equals(fieldType)
    			|| "TIMESTAMP".equals(fieldType)
    			|| "TEXT".equals(fieldType)
    			|| "LONG".equals(fieldType)
    			|| "DATE".equals(fieldType)
    			|| "LONGBLOB".equals(fieldType)
    			|| "NVARCHAR".equals(fieldType)){
    		return "String";
    	} else if("INT".equals(fieldType)
    			|| "SMALLINT".equals(fieldType)
    			|| "MEDIUMINT".equals(fieldType)){
    		return "Integer";
    	} else if("DECIMAL".equals(fieldType)
    			|| "NUMBER".equals(fieldType)
    			|| "BIGINT".equals(fieldType)){
    		return "BigDecimal";    		
    	} else {
    		return fieldType;
    	}
    }
}
